package cn.com.zcode.test;

import java.util.Collection;

import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring.xml" })
public class BaseTestSupport {
	@Autowired
	protected ApplicationContext applicationContext;

	public ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	public void setApplicationContext(ApplicationContext applicationContext) {
		this.applicationContext = applicationContext;
	}
	
	protected void print(Collection<?> col){
		System.out.println("===================");
		for (Object object : col) {
			System.out.println(object);
		}
		System.out.println("===================");
	}
	
	protected void print(Object obj){
		System.out.println("===================");
		System.out.println(obj);
		System.out.println("===================");
	}
	
	protected TxManager beginTransaction(){
		DefaultTransactionDefinition def = new DefaultTransactionDefinition();
		DataSourceTransactionManager txManager = (DataSourceTransactionManager) applicationContext.getBean("transactionManager");
		TransactionStatus status = txManager.getTransaction(def);
		return new TxManager( txManager,status);
	}
	
}
